package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {

        return ResponseResult.okResult(list());
    }

    /**
     * 保存频道
     *
     * @param adchannel
     * @return
     */
    @Override
    public ResponseResult insert(WmChannel adchannel) {
        //1.判断是否为空
        if(adchannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断名称是否为空
        if(adchannel.getName() == null || "".equals(adchannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //3.判断名称是否重复
        WmChannel channel = this.getOne(new QueryWrapper<WmChannel>().eq("name", adchannel.getName()));
        if(channel != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //4.保存


        adchannel.setCreatedTime(new Date());
        adchannel.setIsDefault(false);
        save(adchannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


        /**
         * 分页查询频道
         *
         * @param channelDto
         * @return
         */
        @Override
        public ResponseResult channelList(ChannelDto channelDto) {

            //1.判断是否为空
            if(channelDto == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
            }

            //检查参数
            channelDto.checkParam();

            //2.判断size
            if( channelDto.getSize() > 10){
                channelDto.setSize(10);
            }

            // 创建Lambda查询包装器
            LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<WmChannel>();

            //3.查询
            if(channelDto.getName() != null) {
              // 添加查询条件：频道名称模糊匹配
              wrapper.like(WmChannel::getName, channelDto.getName());
            }

            //创建分页对象
            IPage page = new Page(channelDto.getPage(), channelDto.getSize());

            // 调用分页查询方法
            page(page,wrapper);

            if(page.getTotal() == 0 && page.getPages() != 1) {
               // 查询不到数据，并且不是第一页
               // 修改当前页为第一页
               channelDto.setPage(1);
                IPage  page2 = new Page(channelDto.getPage(), channelDto.getSize());
               page(page2,wrapper);

            }
            // 创建响应结果对象
           ResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) page.getTotal());

            // 设置响应结果的数据为分页查询结果中的记录列表
           responseResult.setData(page.getRecords());

            // 返回响应结果
            return responseResult;
        }


    /**
     * 更新频道数据
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updatechannel(WmChannel adChannel) {
        //1.判断是否为空
        if(adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断id是否为空
        if(adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<WmChannel>().eq(WmChannel::getId, adChannel.getId());
        update(adChannel, wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deletechannel(Integer id) {

        //1.判断是否为空
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.跟据id查询
        WmChannel channel = this.getById(id);
        if(channel.getStatus().equals(true) ){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道正在被使用");
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}